<?php
//系统配置中心
Namespace Api\Controller;
use Think\Controller;
use Api\Model;
 class SystemApiController extends ApiBaseController {
    //获取验证码
    public function getVerificationCode(){
        $mdlSms =new Model\AppSmsModel();
        $strPhone=trim(I("phone"));
        if($strPhone=="")
        {
            ShowJsonReturnAndDie(null,true,"手机号不能为空");
        }
        $arrReturn =  $mdlSms->sendSms($strPhone,I("type"));

        ShowJsonReturnAndDie($arrReturn);
    }
    //用户注册
    public function register(){
        $strPhone = I("phone");
        $strCode = I("code");
        $iID = I("serialNumber");
        $strPwd =I("password");
        $strCode = trim($strCode);
        if(trim($strPhone)=="") ShowJsonReturnAndDie(null,true,"用户名不能为空");
        $mdlSms =new Model\AppSmsModel();
        $strError=  $mdlSms->IsCodeValid($iID,$strCode);
        /* 测试过滤掉验证码
        if($strError!="")
        {
            ShowJsonReturnAndDie(null,true,$strError);
        }
         */
        $mdlParents =new Model\AppParentsModel();
        $arrReturn =$mdlParents->checkAndAddParent($strPhone,$strPwd);
        $strError = $arrReturn["error"];
        $arrUser = $arrReturn["user"];
        if($strError!="")
        {
            ShowJsonReturnAndDie(null,true,$strError);
        }
        $strToken = $mdlParents->getToken($arrUser["id"]);
        ShowJsonReturnAndDie(array("token"=>$strToken),false,"");
    }
    //用户登陆
    public function login(){
        $strPhone  = I("phone");
        $strPwd = I("password");
        //$strToken = I("token");
        //$strPhone="17319357283";
        //$strpwd ="123456";
        $mdlParents =new \Api\Model\AppParentsModel;
        
        if(trim($strPhone)=="")
        {
            ShowJsonReturnAndDie(null,true,"用户名不能为空");
        }
        $arrParents =  $mdlParents->checklogin($strPhone,$strPwd);
        if($arrParents==null)
        {
            ShowJsonReturnAndDie(null,true,"用户名或密码错误");
        }
        
        $iParentID = $arrParents["id"];
        $strToken = $mdlParents->getToken($iParentID);
        $strDefaultUsername = $mdlParents->getDefaultStudentByParentID($iParentID);
        ShowJsonReturnAndDie( array("token"=>$strToken,"defaultUsername"=>$strDefaultUsername));
    }

    //忘记密码/修改密码
    public function resetPassword()
    {
        $strPhone = I("phone");
        $strCode = I("code");
        $iID = I("serialNumber");
        $strPwd =I("password");
        $mdlSms =new Model\AppSmsModel();
        $strError = $mdlSms->IsCodeValid($iID,$strCode);
        if($strError!="")
        {
            ShowJsonReturnAndDie(null,true,$strError);
        }
        $mdlParent =new Model\AppParentsModel();
        $strError = $mdlParent->UpdatePassword($strPhone,$strPwd);
        if($strError!="")
        {
            ShowJsonReturnAndDie(null,true,$strError);
        }
        ShowJsonReturnAndDie(null);
    }
}
